package com.wnhz.work.jy.activity;

import android.animation.ObjectAnimator;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.tencent.connect.share.QQShare;
import com.tencent.tauth.IUiListener;
import com.tencent.tauth.Tencent;
import com.tencent.tauth.UiError;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.wnhz.work.jy.BaseActivity;
import com.wnhz.work.jy.MyApplication;
import com.wnhz.work.jy.R;
import com.wnhz.work.jy.utils.ConfigUtil;
import com.wnhz.work.jy.utils.DensityUtils;
import com.wnhz.work.jy.utils.xutils3.MyCallBack;
import com.wnhz.work.jy.utils.xutils3.XUtil;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 二维码
 */
public class QRCode extends BaseActivity implements View.OnClickListener {
    private static final String TAG = "QRCode";
    private TextView title;
    private ImageView qr_code;
    private TextView prompt_tv;
    private TextView invitation_code_tv;//邀请码
    private TextView register_number_tv;//注册人数
    private TextView purchase_number_tv;//买金金额
    private ScrollView myScroll;

    private int width;
    private int height;

    private List<View> views = new ArrayList<View>();
    private View xinlang, qq, weixin, weixin_moments, share_lay, shape_view;
    private UMImage image = new UMImage(QRCode.this,
            "http://www.umeng.com/images/pic/social/integrated_3.png");

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_qrcode);
        initTirle();
        initView();
        initClick();
        mTencent = Tencent.createInstance("1105763186", this.getApplicationContext());

        initShape();
        setLayHeight();
        getData();
    }

    private void setLayHeight() {
        for (int i = 0; i < views.size(); i++) {
            LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) views.get(i)
                    .getLayoutParams();
            params.height = width / 4;
            views.get(i).setLayoutParams(params);
        }
    }

    private void initShape() {
        WindowManager wm = this.getWindowManager();
        shape_view = findViewById(R.id.shape_view);
        width = wm.getDefaultDisplay().getWidth();
        height = wm.getDefaultDisplay().getHeight()
                + DensityUtils.dp2px(getApplication(), 100);
        ObjectAnimator.ofFloat(share_lay, "translationY", 0, height)
                .setDuration(0).start();
        ObjectAnimator.ofFloat(shape_view, "alpha", 1.0f, 0f).setDuration(0)
                .start();
    }

    private void initClick() {
        share_lay.setOnClickListener(this);
        for (int i = 0; i < views.size(); i++) {
            views.get(i).setOnClickListener(this);
        }

        findViewById(R.id.rule).setOnClickListener(this);
        findViewById(R.id.detail).setOnClickListener(this);
        findViewById(R.id.share_btn).setOnClickListener(this);
    }

    private void initView() {
        myScroll = (ScrollView) findViewById(R.id.myScroll);
        share_lay = findViewById(R.id.share_lay);
        shape_view = findViewById(R.id.shape_view);
        qr_code = (ImageView) findViewById(R.id.qr_code);
        prompt_tv = (TextView) findViewById(R.id.prompt);
        invitation_code_tv = (TextView) findViewById(R.id.invitation_code);
        register_number_tv = (TextView) findViewById(R.id.register_number);
        purchase_number_tv = (TextView) findViewById(R.id.purchase_number);

        share_lay = findViewById(R.id.share_lay);
        weixin_moments = findViewById(R.id.weixin_moments);
        weixin = findViewById(R.id.weixin);
        qq = findViewById(R.id.qq);
        xinlang = findViewById(R.id.xinlang);
        views.add(xinlang);
        views.add(qq);
        views.add(weixin);
        views.add(weixin_moments);
    }

    private void initTirle() {
        title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText("推荐码");
        findViewById(R.id.btn_main_left).setOnClickListener(this);
    }

    private String shapeTitle = "嘉轶金服";
    private String shapeText = "上海嘉轶金融信息服务有限公司是基于资本运作和互联网科技的深度融合平台，致力于为客户提供包括财务咨询、财富管理、金融资讯订制等服务的专业金融信息服务机构。";
    private String shapeUrl= "";
    @Override
    public void onClick(View view) {
        image = new UMImage(QRCode.this,
                qrcode);
        switch (view.getId()){
            case R.id.btn_main_left:
                finish();
                break;
            case R.id.rule://推荐规则
                break;
            case R.id.detail://推荐明细
                break;
            case R.id.share_btn://分享
                showOrHide(false);


                break;
            case R.id.share_lay:
                showOrHide(true);
                break;
            case R.id.xinlang:
                image = new UMImage(QRCode.this,
                        qrcode);
                showOrHide(true);
                new ShareAction(this).setPlatform(SHARE_MEDIA.SINA)
                        .setCallback(umShareListener).withMedia(image)
                        .withText(shapeText).withTargetUrl(shapeUrl)
                        .withTitle(shapeTitle).share();
                //setEnab(false);
                showOrHide(true);
                break;
            case R.id.qq:
                onClickShare();
                showOrHide(true);
                /*image = new UMImage(QRCode.this,
                        qrcode);
                new ShareAction(this).setPlatform(SHARE_MEDIA.QQ)
                        .setCallback(umShareListener).withMedia(image)
                        .withText(shapeText).withTargetUrl(shapeUrl)
                        .withTitle(shapeTitle).share();
                //setEnab(false);
                showOrHide(true);*/
                break;
            case R.id.weixin:
                image = new UMImage(QRCode.this,
                        qrcode);
                //setEnab(false);
                new ShareAction(this).setPlatform(SHARE_MEDIA.WEIXIN)
                        .setCallback(umShareListener).withMedia(image)
                        .withText(shapeText).withTargetUrl(shareUrl)
                        .withTitle(shapeTitle).share();
                showOrHide(true);
                break;
            case R.id.weixin_moments:
                image = new UMImage(QRCode.this,
                        qrcode);
                new ShareAction(this).setPlatform(SHARE_MEDIA.WEIXIN_CIRCLE)
                        .setCallback(umShareListener).withMedia(image)
                        .withText(shapeText).withTargetUrl(shareUrl)
                        .withTitle(shapeTitle).share();
                //setEnab(false);
                showOrHide(true);
                break;
        }
    }

    private Tencent mTencent;;
    private void onClickShare() {
        final Bundle params = new Bundle();
        params.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE, QQShare.SHARE_TO_QQ_TYPE_DEFAULT);
        params.putString(QQShare.SHARE_TO_QQ_TITLE, shapeTitle);
        params.putString(QQShare.SHARE_TO_QQ_SUMMARY,  shapeText);
        params.putString(QQShare.SHARE_TO_QQ_TARGET_URL,  "http://www.qq.com/news/1.html");
        params.putString(QQShare.SHARE_TO_QQ_IMAGE_URL,"http://imgcache.qq.com/qzone/space_item/pre/0/66768.gif");
        params.putString(QQShare.SHARE_TO_QQ_APP_NAME,  shapeTitle);

        mTencent.shareToQQ(QRCode.this, params, new IUiListener() {
            @Override
            public void onComplete(Object o) {

            }

            @Override
            public void onError(UiError uiError) {

            }

            @Override
            public void onCancel() {

            }
        });
    }

    private void showOrHide(boolean isShowShare) {
        if (isShowShare) {
            ObjectAnimator.ofFloat(share_lay, "translationY", 0, height)
                    .setDuration(300).start();
            ObjectAnimator.ofFloat(shape_view, "alpha", 1.0f, 0.0f)
                    .setDuration(300).start();
        } else if (!isShowShare) {
            ObjectAnimator.ofFloat(share_lay, "translationY", height, 0)
                    .setDuration(300).start();
            ObjectAnimator.ofFloat(shape_view, "alpha", 0f, 1.0f)
                    .setDuration(300).start();
        }
    }

    /**
     * 友盟回调监听
     */
    private UMShareListener umShareListener = new UMShareListener() {
        @Override
        public void onResult(SHARE_MEDIA platform) {
            Log.d("plat", "platform" + platform);
            if (platform.name().equals("WEIXIN_FAVORITE")) {
                Toast.makeText(QRCode.this,
                        platform + " 收藏成功啦", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(QRCode.this,
                        platform + " 分享成功啦", Toast.LENGTH_SHORT).show();
            }
            //setEnab(true);
        }

        @Override
        public void onError(SHARE_MEDIA platform, Throwable t) {
            //(true);
            Toast.makeText(QRCode.this, platform + " 分享失败啦",
                    Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onCancel(SHARE_MEDIA platform) {
            //setEnab(true);
            Toast.makeText(QRCode.this, platform + " 分享取消了",
                    Toast.LENGTH_SHORT).show();
        }
    };

    private void setEnab(boolean b) {
        for (int i = 0; i < views.size(); i++) {
            views.get(i).setEnabled(b);
        }
    }

    private String qrcode;
    private String shareUrl;
    private void getData(){
        Map<String,Object> map = new HashMap<>();
        map.put("userId", MyApplication.getInstance().userBean.getUserId());
        showDialog();
        XUtil.Post(ConfigUtil.REFERRAL,map,new MyCallBack<String>(){
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.d(TAG, "onSuccess() returned: " + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    qrcode = jsonObject.optString("qrcode");
                    String recommended = jsonObject.optString("recommended");
                    shareUrl = jsonObject.optString("shareUrl");
                    myScroll.setVisibility(View.VISIBLE);
                    invitation_code_tv.setText(recommended);
                    prompt_tv.setText("下载“嘉轶金服”，注册时请填写邀请码: "+recommended);
                    x.image().bind(qr_code,qrcode);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                super.onFinished();
                closeDialog();
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }
}
